//
// Created by Aimeow on 2022/12/1.
//

import Foundation
import HandyJSON

// MARK: - 保安管理
// MARK: - URL

let URLSecurityCheckSign = "/security/check_sign" // 签到验证
let URLSecurityClockIn  = "/v2/security/clock_in" // 签到/签退
let URLSecurityBmQdWcQk  = "/security/selectBmQdWcQk" // 根据部门查询签到完成情况
let URLSecurityScaleByDept = "/security/selectScaleByDept" // 根据部门查询完成率
let URLSecurityStatics = "/security/statics" // 考勤统计

let URLLzglbaListByDept = "/lzglba/selectListByDept" // 根据部门查询任务列表
let URLLzglbaWccCount = "/lzglba/getWccCount" // 保安任务列表统计
let URLLzglbaTasklist = "/lzglba/tasklist" // 保安任务列表
let URLLzglbaRwedit = "/lzglba/rwedit" // 保安任务详情
let URLLzglbaRwsave = "/lzglba/rwsave" // 保安履职任务保存

let URLBaglQueryBaglByDept = "/bagl/queryBaglByDept" // 在岗情况
let URLBaglQueryBalzByDept = "/bagl/queryBalzByDept" // 履职情况
let URLBaglQueryRwwcqk = "/bagl/queryRwwcqk"  // 各任务完成情况
let URLBaglQueryRwwcjd = "/bagl/queryRwwcjd" // 任务完成进度
let URLBaglQuerySignInfo = "/bagl/querySignInfo" // 当日考勤记录
let URLBaglQuerySecurityInfo = "/bagl/querySecurityInfo" // 保安统计情况

struct SecurityParam: Encodable {
    var id: Int64?
    
    var deptId: Int64?
    var dateType: Int?
    var date: String?
    
    var ownerId: String?
    var rwid: String?

    var pageNum: Int?
    var pageSize: Int?
    
    var timestamp: String?
    var signature: String?
    
    init(id: Int64? = nil, deptId: Int64? = nil, dateType: Int? = nil, date: String? = nil, ownerId: String? = nil, rwid: String? = nil, pageNum: Int? = nil, pageSize: Int? = nil) {
        self.id = id
        self.deptId = deptId
        self.dateType = dateType
        self.date = date
        self.ownerId = ownerId
        self.rwid = rwid
        self.pageNum = pageNum
        self.pageSize = pageSize
    }
}

struct ClockInParam: Encodable {
//    var imgStr: String?
    var lngLat: String?
    var location: String?
    var signDeptId: Int64?
    
    var timestamp: String?
    var signature: String?

//    init(imgStr: String? = nil, lngLat: String? = nil, location: String? = nil, signDeptId: Int64? = nil) {
//        self.imgStr = imgStr
//        self.lngLat = lngLat
//        self.location = location
//        self.signDeptId = signDeptId
//    }
    init(lngLat: String? = nil, location: String? = nil, signDeptId: Int64? = nil) {
        self.lngLat = lngLat
        self.location = location
        self.signDeptId = signDeptId
    }
}

struct SecurityStaticsParam: Encodable {
    var day: Int?
    var month: Int?
    var year: Int?
    
    var timestamp: String?
    var signature: String?
    
    init(day: Int? = nil, month: Int? = nil, year: Int? = nil) {
        self.day = day
        self.month = month
        self.year = year
    }
}

// 保安管理
// MARK: - API

extension API {

    // 签到验证
    class func getCheckSign(block: ResponseBlock<SignVerifyModel>?) {
        let param = getSign()
        NetManager.request(url: URLSecurityCheckSign, method: .get, parameters: param, modelType: SignVerifyModel.self, resultBlock: block)
    }

    // 签到/签退
    class func postClockIn(withJsonStr jsonStr: String, imagePath: String, block: ResponseBlock<ClockInModel>?) {
//        NetManager.request(url: URLSecurityClockIn, parameters: param, modelType: ClockInModel.self, resultBlock: block)
        let imgUrl = URL(fileURLWithPath: imagePath)
        let signP = getSign()
        NetManager.uploadWithParam(url: URLSecurityClockIn, multipartFormData: { multiPart in
            multiPart.append(jsonStr.data(using: .utf8) ?? Data(), withName: "jsonStr")
            multiPart.append(imgUrl, withName: "file")
            multiPart.append(signP.timestamp?.data(using: .utf8) ?? Data(), withName: "timestamp")
            multiPart.append(signP.signature?.data(using: .utf8) ?? Data(), withName: "sign")
        }, modelType: ClockInModel.self, resultBlock: block)
    }

    // 考勤统计
    class func getSecurityStatics(withParam _param: SecurityStaticsParam, block: ResponseBlock<SecurityStaticsModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLSecurityStatics, method: .get, parameters: param, modelType: SecurityStaticsModel.self, resultBlock: block)
    }

    // 根据部门查询签到完成情况
    class func getSecurityBmQdWcQk(withParam _param: SecurityParam, block: ResponseBlock<SecurityBmQdWcQkModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLSecurityBmQdWcQk, method: .get, parameters: param, modelType: SecurityBmQdWcQkModel.self, resultBlock: block)
    }

    // 根据部门查询完成率
    class func getSecurityScaleByDept(withParam _param: SecurityParam, block: ResponseBlock<CommonScaleByDeptModal>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLSecurityScaleByDept, method: .get, parameters: param, modelType: CommonScaleByDeptModal.self, resultBlock: block)
    }
    
    // 根据部门查询任务列表
    class func getLzglbaListByDept(withParam _param: SecurityParam, block: ResponseBlock<LzglbaListModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLLzglbaListByDept, method: .get, parameters: param, modelType: LzglbaListModel.self, resultBlock: block)
    }
    
    // 保安任务列表统计
    class func getLzglbaWccCount(withBlock block: ResponseBlock<LzglWccCountModel>?) {
        let param = getSign()
        NetManager.request(url: URLLzglbaWccCount, method: .get, parameters: param, modelType: LzglWccCountModel.self, resultBlock: block)
    }
    
    // 保安任务列表
    class func getLzglbaTasklist(withParam _param: SecurityParam, block: ResponseBlock<LzglbaListModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLLzglbaTasklist, method: .get, parameters: param, modelType: LzglbaListModel.self, resultBlock: block)
    }
    
    // 保安任务详情
    class func getLzglbaRwedit(withParam param: SecurityParam, block: ResponseBlock<LzglbaListModel>?) {
        NetManager.request(url: URLLzglbaRwedit, method: .get, parameters: param, modelType: LzglbaListModel.self, resultBlock: block)
    }
    
    // 保安履职任务保存
    class func postLzglbaRwsave(withParam _param: CommonJsonParam, block: ResponseBlock<TaskSaveModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLLzglbaRwsave, parameters: param, modelType: TaskSaveModel.self, resultBlock: block)
    }
        
    // 在岗情况
    class func getBaglByDept(withParam _param: SecurityParam, block: ResponseBlock<BaglByDeptModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQueryBaglByDept, method: .get, parameters: param, modelType: BaglByDeptModel.self, resultBlock: block)
    }
    
    // 履职情况
    class func getBaglBalzByDept(withParam _param: SecurityParam, block: ResponseBlock<BalzByDeptModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQueryBalzByDept, method: .get, parameters: param, modelType: BalzByDeptModel.self, resultBlock: block)
    }
    
    // 各任务完成情况
    class func getBaglRwwcqk(withParam _param: SecurityParam, block: ResponseBlock<BaglRwwcqkModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQueryRwwcqk, method: .get, parameters: param, modelType: BaglRwwcqkModel.self, resultBlock: block)
    }
    
    // 任务完成进度
    class func getBaglRwwcjd(withParam _param: SecurityParam, block: ResponseBlock<BaglRwwcjdModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQueryRwwcjd, method: .get, parameters: param, modelType: BaglRwwcjdModel.self, resultBlock: block)
    }
    
    // 当日考勤记录
    class func getBaglSignInfo(withParam _param: SecurityParam, block: ResponseBlock<BaglSignInfoModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQuerySignInfo, method: .get, parameters: param, modelType: BaglSignInfoModel.self, resultBlock: block)
    }
    
    // 保安统计情况
    class func getBaglSecurityInfo(withParam _param: SecurityParam, block: ResponseBlock<BaglSecurityInfoModel>?) {
        let signP = getSign(param: _param)
        var param = _param
        param.timestamp = signP.timestamp
        param.signature = signP.signature
        NetManager.request(url: URLBaglQuerySecurityInfo, method: .get, parameters: param, modelType: BaglSecurityInfoModel.self, resultBlock: block)
    }
}

// MARK: - Modals

// 在岗情况
struct BaglByDeptModel: HandyJSON {
    var zgl: String?  // 总在岗率
    var wdList: [BaglByDeptWdModel]?
    var title: String?  // 过去七天整体在岗率
    var zxList: [BaglByDeptZxModel]?
}

struct BaglByDeptWdModel: HandyJSON {
    var wddm: String?
    var wdmc: String?
    var wdlxr: String?
    var wdlxrhm: String?
    var deptPosition: String?
    var zwcl: String?
    var bazgrs: Int64? // 在岗人数
    var percent: String?
    var sfczzj: Bool?
    var sfyy: Bool?
}

struct BaglByDeptZxModel: HandyJSON {
    var date: String?
    var dateTypeTitle: String?
    var zwcl: String?
}

// 履职情况
struct BalzByDeptModel: HandyJSON {
    var zwcl: String?  // 总完成率
    var wdList: [BalzByDeptWdModel]?
    var title: String?  // 过去七天整体在岗率
    var zxList: [BaglByDeptZxModel]?
}

struct BalzByDeptWdModel: HandyJSON {
    var lzrmc: String?
    var wcl: String?
    var ownerId: Int64? // 保安ID
    var sfyy: Bool?
}

// 各任务完成情况
struct BaglRwwcqkModel: HandyJSON {
    var wcTotal: Int64? //完成总数
    var wwcTotal: Int64? //未完成总数
    var total: Int64? //总数
    var percent: Int64? //完成率
    var taskId: Int64? //任务ID
    var taskName: String?
}

// 任务完成进度
struct BaglRwwcjdModel: HandyJSON {
    var rwxq: BaglRwwcjdRwxqModel?
    var lzrArr: [BaglRwwcjdLzrModel]?
}

struct BaglRwwcjdRwxqModel: HandyJSON {
    var taskId: Int64?
    var taskName: String?
    var startTime: String?
    var endTime: String?
    var total: Int64?
    var wcTotal: Int64?
    var wwcTotal: Int64?
}

struct BaglRwwcjdLzrModel: HandyJSON {
    var lzrid: Int64?
    var lzrmc: String?
    var wcFlag: Bool?
}

// 当日考勤记录
struct BaglSignInfoModel: HandyJSON {
    var ownerId: Int64? //用户ID
    var nickName: String? //用户名称
    var qdsj: String? //签到时间
    var qtsj: String? //签退时间
    var zggs: String? //在岗时长
}

// 保安统计情况
struct BaglSecurityInfoModel: HandyJSON {
    var qdwc: BaglSecurityInfoQdwcModel?
    var rwjl: [BaglSecurityInfoRwjlModel]?
    var rwwcqk: BaglSecurityInfoRwwcqkModel?
}

struct BaglSecurityInfoQdwcModel: HandyJSON {
    var qdwcl: String? //签到完成率
    var qds: Int64? //签到数
    var wqds: Int64? //签到异常
}

struct BaglSecurityInfoRwjlModel: HandyJSON {
    var id: Int64? //任务记录ID
    var rwid: Int64? //任务ID
    var rwmbid: Int64? //任务模板ID
    var lzrid: Int64? //履职人ID
    var lzrmc: String? //履职人名称
    var wddm: Int64? //网点代码
    var wdmc: String? //网点名称
    var lzrhm: String?
    var rwlxdm: String?
    var rwlxmc: String?
    var rwmc: String?
    var rwzq: String?
    var rwzxkssj: String?
    var rwzxssj: String?
    var rwzt: String? //0：待完成，1：按期完成，2：超前完成 3：超期完成 4：超期未完成 5 未完成
    var yczt: String?
    var status: String?
    var rwrq: String?
    var tjsj: String?
    var rwkssj: String?
    var rwjssj: String?
    var ancestors: String?
    var batched: Bool?
}

struct BaglSecurityInfoRwwcqkModel: HandyJSON {
    var rwzs: Int64? //任务总数
    var wcs: Int64? //完成数
    var wwcs: Int64? //未完成数
    var zwcl: Int64? //完成率
}

struct SecurityBmQdWcQkModel: HandyJSON {
    var wqds: Int64?
    var qdwcl: Double?
    var ywcs: String?
    var rwwcl: String?
    var wwcs: String?
    var rws: String?
    var qds: Int64?
}

struct LzglbaListModel: HandyJSON {
    var EndTime: String?
    var StartTime: String?
    var batched: Int64?
    var createBy: String?
    var createTime: String?
    var czrId: String?
    var endDate: String?
    var endTime: String?
    var id: Int64?
    var lat: String?
    var lng: String?
    var location: String?
    var lzrhm: String?
    var lzrid: Int64?
    var lzrmc: String?
    var remark: String?
    var rwid: Int64?
    var rwjlMbDqList: [SRwjlMbDqModal]?
    var rwjlMbList: [SRwjlMbModal]?
    var rwjssj: String?
    var rwkssj: String?
    var rwlxdm: Int64?
    var rwlxmc: String?
    var rwmbList: String?
    var rwmbid: String?
    var rwmc: String?
    var rwqm: String?
    var rwrq: String?
    var rwzq: Int64?
    var rwzt: String?
    var rwzxkssj: String?
    var rwzxssj: String?
    var startDate: String?
    var startTime: String?
    var status: String?
    var tjsj: String?
    var wddm: Int64?
    var wdmc: String?
    var yczt: String?
}

struct SRwjlMbDqModal: HandyJSON {
    var boscLzglRwjlMbs: [SBoscLzglRwjlModal]?
    var boscLzglRwmbs: String?
    var dqmc: String?
    var id: Int64?
    var nfcwz: String?
    var remark: String?
    var rwlxdm: String?
}

struct SBoscLzglRwjlModal: HandyJSON {
    var answers: String?
    var dqdm: String?
    var dqmc: String?
    var endDate: String?
    var id: Int64?
    var images: [String]?
    var nfcwz: String?
    var optionLable: String?
    var optionValue: String?
    var remark: String?
    var rwid: Int64?
    var rwjlid: String?
    var rwlxdm: Int64?
    var rwlxmc: String?
    var rwmbBqzzbList: [SRwmbBqzzbModal?]?
    var rwmbid: String?
    var rwmc: String?
    var rwzt: String?
    var sfzg: String?
    var startDate: String?
    var tjsj: String?
    var wddm: String?
    var xxlxdm: String?
    var xxlxdmc: String?
    var xxlxpz: String?
}

struct SRwjlMbModal: HandyJSON {
    var answers: String?
    var dqdm: Int64?
    var dqmc: String?
    var endDate: String?
    var id: Int64?
    var images: [String]?
    var nfcwz: String?
    var optionLable: String?
    var optionValue: String?
    var remark: String?
    var rwid : Int64?
    var rwjlid: String?
    var rwlxdm: Int64?
    var rwlxmc: String?
    var rwmbBqzzbList: String?
    var rwmbid: String?
    var rwmc: String?
    var rwzt: String?
    var searchValue: String?
    var sfzg: String?
    var startDate: String?
    var tjsj: String?
    var wddm: String?
    var xxlxdm: String?
    var xxlxdmc: String?
    var xxlxpz: String?
}

struct SRwmbBqzzbModal: HandyJSON {
    var bqid: Int64?
    var bqmc: String?
    var mbid: String?
    var pdbz: Int64?
    var remark: String?
}

struct LzglWccCountModel: HandyJSON {
    var nrws: Int64? //年任务数
    var jrws: Int64? //季任务数
    var rrws: Int64? //日任务数
    var yrws: Int64? //月任务数
}
